package gui.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Field;
import java.util.Arrays;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import gui.Context;
import gui.parser.Attr;
import gui.parser.ColorFactory;

public class View extends Context {
	protected int id;
	protected Attr attr;
	public static final boolean VISIBLE = true;
	public static final boolean INVISIBLE = false;
	public static final String CENTER = "center";
	public static final String LEFT = "left";
	public static final String RIGHT = "right";
	public static final String TOP = "top";
	public static final String BOTTOM = "bottom";
	public static final String CENTER_HORIZONTAL = "center_horizontal";
	public static final String CENTER_VERTICAL = "center_vertical";
	public boolean visible = VISIBLE;
	public int width;
	public int height;
	public int x;
	public int y;
	public int paddingLeft;
	public int paddingRight;
	public int paddingTop;
	public int paddingBottom;
	public int margin_top;
	public int margin_bottom;
	public int margin_left;
	public int margin_right;
	public String layout_gravity;
	public String grivity;
	protected boolean grivity_center_horizontal;
	protected boolean grivity_center_vertical;
	protected boolean grivity_left;
	protected boolean grivity_top;
	protected boolean grivity_bottom;
	protected boolean grivity_right;
	protected int bacground_resource;
	protected Color bacground_color;
	private ViewGroup parent;
	/**
	 * 活动状态
	 */
	private boolean enable;
	/**
	 * 是否可点击
	 */
	private boolean clickable;

	protected OnClickListener onClickListener;

	public View(Attr attr) {
		this.attr = attr;
		init();
		if (isEnable())
			addAllListener();
	}

	/**
	 * 添加监听器
	 */
	protected void addAllListener() {
		getView().addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				if (clickable)
					if (onClickListener != null)
						onClickListener.onClick(View.this);
			}
		});
	}

	public Component getView() {
		return null;
	}

	public Container getLayout() {
		return null;
	}

	public int getHeight() {
		return height;
	}

	public int getWidth() {
		return width;
	}

	public ViewGroup getParent() {
		return parent;
	}

	/**
	 * 解析配置文件参数,仅初始化时调用一次
	 */
	public void init() {
		String m;
		m = getAttr("gui:id");
		if (m != null && m.startsWith("@+id/")) {
			m = m.substring(m.indexOf("@+id/") + 5, m.length());
			try {
				Field field = Class.forName("gen.R$id").getDeclaredField(m);
				if (field != null)
					id = field.getInt(null);
			} catch (Exception e) {
			}
		}
		m = getAttr("gui:layout_width");
		if (m.equals(MATCH_PARENT))
			width = -1;
		else
			width = Integer.parseInt(m);
		m = getAttr("gui:layout_height");
		if (m.equals(MATCH_PARENT))
			height = -1;
		else
			height = Integer.parseInt(m);
		layout_gravity = getAttr("gui:layout_gravity");
		grivity = getAttr("gui:grivity");
		if (grivity != null) {
			String[] split = grivity.split("\\|");
			Arrays.sort(split);
			grivity_center_horizontal = grivity_center_vertical = (Arrays.binarySearch(split, CENTER) >= 0);
			if (!grivity_center_horizontal) {
				grivity_center_horizontal = (Arrays.binarySearch(split, CENTER_HORIZONTAL) >= 0);
				grivity_center_vertical = (Arrays.binarySearch(split, CENTER_VERTICAL) >= 0);
			}
			grivity_left = (Arrays.binarySearch(split, LEFT) >= 0);
			grivity_right = (Arrays.binarySearch(split, RIGHT) >= 0);
			grivity_top = (Arrays.binarySearch(split, TOP) >= 0);
			grivity_bottom = (Arrays.binarySearch(split, BOTTOM) >= 0);
		}
		margin_bottom = margin_left = margin_right = margin_top = getIntAttr("gui:margin", 0);
		margin_top = getIntAttr("gui:margin_top", margin_top);
		margin_bottom = getIntAttr("gui:margin_bottom", margin_bottom);
		margin_left = getIntAttr("gui:margin_left", margin_left);
		margin_right = getIntAttr("gui:margin_right", margin_right);
		paddingBottom = paddingLeft = paddingRight = paddingTop = getIntAttr("gui:padding", 0);
		paddingBottom = getIntAttr("gui:paddingBottom", paddingBottom);
		paddingLeft = getIntAttr("gui:paddingLeft", paddingLeft);
		paddingRight = getIntAttr("gui:paddingRight", paddingRight);
		paddingTop = getIntAttr("gui:paddingTop", paddingTop);
		x = getIntAttr("gui:x", 0);
		y = getIntAttr("gui:y", 0);
		m = getAttr("gui:background");
		if (m != null) {
			if ((bacground_color = ColorFactory.build(m)) != null) {

			} else if (m.startsWith("@image")) {
				bacground_resource = getResource().getResourceID(m);
			}
		}
	}

	/**
	 * 刷新布局信息
	 */
	public void invalidate() {
		if (this instanceof ViewGroup) {
			if (bacground_resource != 0) {
				getLayout().add(new JLabel(getResource().getImage(bacground_resource)));
			} else if (bacground_color != null) {
				JLabel jLabel = new JLabel();
				jLabel.setBackground(bacground_color);
				getLayout().add(jLabel);
			} else {
				JLabel jLabel = new JLabel();
				getLayout().add(jLabel);
			}
		} else if (bacground_color != null) {
			getView().setBackground(bacground_color);
		}
	}

	public boolean isEnable() {
		return enable;
	}

	public boolean isClickable() {
		return clickable;
	}

	/**
	 * 获取整数型参数
	 * 
	 * @param key
	 * @return
	 */
	protected int getIntAttr(String key, int def) {
		return attr.getIntAttr(key, def);
	}

	/**
	 * 获取boolean
	 * 
	 * @param key
	 * @return
	 */
	protected boolean getBooleanAttr(String key, boolean def) {
		return attr.getBooleanAttr(key, def);
	}

	/**
	 * 获取字符串参数
	 * 
	 * @param key
	 * @return
	 */
	protected String getAttr(String key) {
		return attr.get(key);
	}

	/**
	 * 设置布局尺寸
	 */
	protected void setBounds() {
		if (this instanceof ViewGroup) {
			getView().setBounds(0, 0, width, height);
			getLayout().setBounds(x, y, width, height);
		} else {
			getView().setBounds(x, y, width, height);
		}
	}

	/**
	 * 设置点击状态
	 * 
	 * @param clickable
	 */
	public void setClickable(boolean clickable) {
		if (clickable)
			setEnable(clickable);
		this.clickable = clickable;
	}

	/**
	 * 设置活动状态
	 * 
	 * @param enable
	 */
	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	/**
	 * 设置点击事件监听器
	 * 
	 * @param onClickListener
	 */
	public void setOnClickListener(OnClickListener onClickListener) {
		this.onClickListener = onClickListener;
	}

	/**
	 * 修改布局
	 */
	protected void onMeasure(int new_x, int new_y, int new_height, int new_width) {
		x = new_x;
		y = new_y;
		height = new_height == 0 ? height : new_height;
		width = new_width == 0 ? width : new_width;
	}

	public void visible(boolean isVisible) {
		visible = isVisible;
	}

	public void setBacgroundColor(Color color) {
		this.bacground_color = color;
		bacground_resource = 0;
		invalidate();
	}

	public Color getBacgroundColor() {
		return bacground_color;
	}

	public void setBacground(int resId) {
		this.bacground_resource = resId;
		bacground_color = null;
		invalidate();
	}

	public ImageIcon getBacground() {
		return getResource().getImage(bacground_resource);
	}

	public void setParent(ViewGroup parent) {
		this.parent = parent;
	}

	public int getId() {
		return id;
	}

	public static interface OnClickListener {
		void onClick(View v);
	}
}
